package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.EpcInfo;
import com.txsj.scan.utils.LabelUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/12/7
 * @Describe
 */
public class EpcAdapter extends RecyclerView.Adapter<EpcAdapter.EpcViewHolder> {
    private Context context;
    private List<EpcInfo> list;
    private IOnItemClickListener onItemClickListener;

    public EpcAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public EpcAdapter(Context context, List<EpcInfo> list) {
        this.context = context;
        this.list = list;
    }

    public IOnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(IOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setList(List<EpcInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public EpcViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new EpcViewHolder(LayoutInflater.from(context).inflate(R.layout.item_epc_new, parent, false));
    }

    @Override
    public void onBindViewHolder(EpcViewHolder holder, int position) {
        holder.bind(list.get(position), position);
    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    public class EpcViewHolder extends RecyclerView.ViewHolder {

        View rootView;
        TextView tv_classify_num, tv_group_num, tv_pipeline_num;
        TextView tv_delete_epc;

        public EpcViewHolder(View itemView) {
            super(itemView);
            this.rootView = itemView;
            tv_classify_num = (TextView) itemView.findViewById(R.id.tv_classify_num);
            tv_group_num = (TextView) itemView.findViewById(R.id.tv_group_num);
            tv_pipeline_num = (TextView) itemView.findViewById(R.id.tv_pipeline_num);
            tv_delete_epc = (TextView) itemView.findViewById(R.id.tv_delete_epc);
        }

        public void bind(EpcInfo t, final int position) {
            if (position % 2 == 0) {
                rootView.setBackgroundColor(context.getResources().getColor(R.color.color_f4));
            } else {
                rootView.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
            EpcInfo info = (EpcInfo) t;

            String strEpc = info.getStrEpc();//此时的strEpc已经没有了空格
//            //解析
//            String epcCode = strEpc.substring(9, strEpc.length());
//            //场景调节数
//            long sceneNumber = Integer.parseInt(epcCode.substring(0, 4), 16);
//            //分类ID
//            long classId = Integer.parseInt(epcCode.substring(4, 6), 16);
//            //流水号
//            long serialNumber = Integer.parseInt(epcCode.substring(6, epcCode.length()), 16);
//            //分组标签数量
//            long groupTagNumber = (long) Math.pow(2, classId);
//            //当前场景下的每组标签数量
//            long sceneGroupTagNumber = groupTagNumber - sceneNumber;
//            //分组id
//            long groupId = (serialNumber / groupTagNumber) + 1;
//            //m_groupSerialNumber = (long)(SerialNumber % GroupTagNumber) + 1;
//            //当前场景下的流水号
//            long serialTagNumber = serialNumber % groupTagNumber + 1;

            tv_classify_num.setText(LabelUtils.getClassId(strEpc) + "");
            tv_group_num.setText(LabelUtils.getGroupId(strEpc) + "");
            tv_pipeline_num.setText(LabelUtils.getSceneGroupTagNumber(strEpc));
            tv_delete_epc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onDelete(position);
                    }
                }
            });
        }
    }


    public interface IOnItemClickListener {
        void onDelete(int position);
    }
}
